﻿/**
 * tree
 * @type {{view: {selectedMulti: boolean, expandSpeed: string}, data: {simpleData: {enable: boolean, idKey: string, pIdKey: string, rootId: string}, key: {name: string}}, callback: {onClick: clickTree}}}
 */
var tree_obj;
var treeNodes_obj;
var zTree_obj;
var treeChecked;
var _idKey="id1";
var _pIdKey="parentId1";
var _name="name1";
var _clickTree=true;
var tree_Obj;
var text;
var code;
var kindId;
var nodes;
var setting;
var _url;
var _fnClick;
var _fnCheck;
var zTreeOnCheck=function (e, treeId, treeNode, clickFlag) {
    debugger;
    // treeNode.tId + ", " + treeNode.name + "," + treeNode.checked
    // zTree.checkNode(treeNode, !treeNode.checked, true);
};
var zTreeOnRightClick=function (e, treeId, treeNode, clickFlag) {
    debugger;

};
var _fn=function clickTree(event, treeId, treeNode) {
    debugger;
    // tree_Obj.checkNode(treeNode, !treeNode.checked, true);
    text = treeNode.name;
    var id=0;
    var isArea=0;
//            code = treeNode.catCode;
    if(_clickTree){
        kindId = treeNode.id;
    }else{
        kindId = treeNode.tid;
        id=treeNode.id;
        isArea=treeNode.isArea;
    }


    nodes = treeNode;
    // if(_clickTree || (!_clickTree&&isArea==1)||(!_clickTree&&id==-1)){
    //     if(_url){
    //         $('#mainGrid').datagrid({
    //             url:_url,
    //             queryParams: {
    //                 kindId: kindId
    //             }
    //         });
    //     }else{
    //         $('#mainGrid').datagrid({
    //             queryParams: {
    //                 kindId: kindId
    //             }
    //         });
    //     }
    //
    // }

    isSelect = true;
}

/**
 *
 * @param id 当前节点id
 * @param pid 父节点id
 * @param name 显示名称
 */
function init_tree(id,pid,name,fnClick,fnCheck,chkStyle,OnRightClick){
    _idKey=id;
    _pIdKey=pid;
    _name=name;
    // _clickTree=flag;
    if(fnClick){
        _fnClick=fnClick;
    }
    if(fnCheck){
        _fnCheck=fnCheck;
    }
    var check={
        enable: false,
        // chkboxType: { "Y": "p", "N": "s" }
    };
    if(chkStyle){
        check={
            enable: true,
            chkStyle: chkStyle,
            // chkboxType: { "Y": "p", "N": "s" }
        };
    }
    if(OnRightClick){
        zTreeOnRightClick=OnRightClick
    }
    setting = {
        view: {
            fontCss: getFontCss,
            selectedMulti: false,
            expandSpeed: "fast"
        },
        data: {
            simpleData: {
                enable: true,
                idKey: _idKey,
                pIdKey: _pIdKey,
                rootId: ""
            },
            key: {
                name: _name
            }
        },
        check: check,
        callback: {
            onClick: _fnClick,
            onCheck: _fnCheck,
            onRightClick: zTreeOnRightClick
        }
    }
}
function loadMyTree(url,treeid,asyncK) {
    var ztreeId="kindTree";
    if(treeid){
        ztreeId=treeid;
    }
    $.ajax({
        async: asyncK ? asyncK : false,
        url: url,
        type: "post",
        dataType: 'json',
        success: function (data) {
            treeNodes_obj = data;
            for (var key in treeNodes_obj) {
                if(key<=1){
                    treeNodes_obj[key].open = true;
                }
            }

            zTree_obj = $.fn.zTree.init($("#"+ztreeId), setting, treeNodes_obj);
            tree_Obj = $.fn.zTree.getZTreeObj(ztreeId);
            var node = tree_Obj.getNodeByParam("id", -1, null);
            tree_Obj.selectNode(node);
            // tree_Obj.expandAll(true);
            if(map){cleanAll();}


            // color
            // for(var i=0;i<treeNodes_obj.length;i++){
            //     var color=""
            //     if(treeNodes_obj[i].warningType=="0"){
            //
            //     }else if(treeNodes_obj[i].warningType=="1"){
            //
            //     }else if(treeNodes_obj[i].warningType=="2"){
            //
            //     }else if(treeNodes_obj[i].warningType=="3"){
            //
            //     }else if(treeNodes_obj[i].warningType=="5"){
            //
            //     }
            //
            //     // tree_Obj.setting.view.fontCss["color"]=
            //     //
            //     // tree_Obj.updateNode(treeNodes_obj[i]);
            // }

        }
    })
}
function loadFenceTree(url,treeid) {
    var ztreeId="kindTree";
    if(treeid){
        ztreeId=treeid;
    }
    $.ajax({
        async: false,
        url: url,
        type: "post",
        dataType: 'json',
        success: function (data) {
            treeNodes_obj = data;
            for (var key in treeNodes_obj) {
                treeNodes_obj[key].open = true;
            }
            zTree_obj = $.fn.zTree.init($("#kindTree"), setting, treeNodes_obj);
            tree_Obj = $.fn.zTree.getZTreeObj(ztreeId);
            var node = tree_Obj.getNodeByParam("id", -1, null);
            tree_Obj.selectNode(node);

        }
    })
}
function getFontCss(treeId, treeNode) {
    var cls="";
    var color="";
    if(!!treeNode.highlight){
        cls={color:"#46a647", "font-weight":"bold"};
        return cls;
    }
    if(treeNode.warningType=="0"){
        color="#333"
    }else{
        color="#1c4a9a"
    }
    if(!treeNode.isCar){
        color="#333"
    }
    return {color:color, "font-weight":"bold"};
    // return (!!treeNode.highlight) ? {color:"#5579a6", "font-weight":"bold"} : {color:"#333", "font-weight":"normal"};
}

/**
 * 展开树
 * @param treeId
 */
function expand_ztree(treeId){
    var treeObj = $.fn.zTree.getZTreeObj(treeId);
    treeObj.expandAll(true);
}

/**
 * 收起树：只展开根节点下的一级节点
 * @param treeId
 */
function close_ztree(treeId){
    // var treeObj = $.fn.zTree.getZTreeObj(treeId);
    // var nodes = treeObj.transformToArray(treeObj.getNodes());
    // var nodeLength = nodes.length;
    // for (var i = 0; i < nodeLength; i++) {
    //     // if (nodes[i].id == '0') {
    //     //     //根节点：展开
    //     //     treeObj.expandNode(nodes[i], true, true, false);
    //     // } else {
    //     //     //非根节点：收起
    //         treeObj.expandNode(nodes[i], false, true, false);
    //     // }
    // }
    // $.fn.zTree.getZTreeObj("treeDemo").expandAll(false);
    var treeObj = $.fn.zTree.getZTreeObj(treeId);
    treeObj.setting.view.expandSpeed = "";
    treeObj.expandAll(false);
    treeObj.setting.view.expandSpeed = "fast";
    // treeObj.expandAll(false);
}

/**
 * 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
 * @param treeId
 * @param searchConditionId 文本框的id
 */
function search_ztree(treeId, searchConditionId){
    searchByFlag_ztree(treeId, searchConditionId, "");
}

/**
 * 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
 * @param treeId
 * @param searchConditionId     搜索条件Id
 * @param flag                  需要高亮显示的节点标识
 */
function searchByFlag_ztree(treeId, searchConditionId, flag){
    //<1>.搜索条件
    var searchCondition = $('#' + searchConditionId).val();
    //<2>.得到模糊匹配搜索条件的节点数组集合
    var highlightNodes = new Array();
    if (searchCondition != "") {
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        highlightNodes = treeObj.getNodesByParamFuzzy("name", searchCondition, null);
    }
    //<3>.高亮显示并展示【指定节点s】
    highlightAndExpand_ztree(treeId, highlightNodes, flag);
}

/**
 * 高亮显示并展示【指定节点s】
 * @param treeId
 * @param highlightNodes 需要高亮显示的节点数组
 * @param flag           需要高亮显示的节点标识
 */
function highlightAndExpand_ztree(treeId, highlightNodes, flag){
    debugger;
    var treeObj = $.fn.zTree.getZTreeObj(treeId);
    //<1>. 先把全部节点更新为普通样式
    var treeNodes = treeObj.transformToArray(treeObj.getNodes());
    for (var i = 0; i < treeNodes.length; i++) {
        treeNodes[i].highlight = false;
        treeObj.updateNode(treeNodes[i]);
    }
    //<2>.收起树, 只展开根节点下的一级节点
    close_ztree(treeId);

    //<3>.把指定节点的样式更新为高亮显示，并展开
    if (highlightNodes != null) {

            for (var i = 0; i < highlightNodes.length; i++) {
                if (flag != null && flag != "") {
                    if (highlightNodes[i].flag == flag) {
                        //高亮显示节点，并展开
                        highlightNodes[i].highlight = true;
                        treeObj.updateNode(highlightNodes[i]);
                        //高亮显示节点的父节点的父节点....直到根节点，并展示
                        var parentNode = highlightNodes[i].getParentNode();
                        // var parentNodes = getParentNodes_ztree(treeId, parentNode);
                        // treeObj.expandNode(parentNodes, true, false, true);
                        // treeObj.expandNode(parentNodess, true, false, true);
                        treeObj.expandNode(parentNode, true, false, true);
                    }
                } else {
                    debugger
                    //高亮显示节点，并展开
                    highlightNodes[i].highlight = true;
                    treeObj.updateNode(highlightNodes[i]);
                    //高亮显示节点的父节点的父节点....直到根节点，并展示
                    var parentNode = highlightNodes[i].getParentNode();
                    treeObj.expandNode(parentNode, true, false, true);
                }
                tree_Obj.checkNode(highlightNodes[i], true, true);
                selectTreeNode && selectTreeNode(highlightNodes[i]);
            }

    }
}
/**
 * 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
 * @param treeId
 * @param searchConditionId 文本框的id
 */
function search_ztree_content(treeId, key,value){
    return  searchByFlag_ztree_content(treeId, key, value,false,'');
}

/**
 * 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
 * @param treeId
 * @param searchConditionId     搜索条件Id
 * @param flag                  需要高亮显示的节点标识
 */
function searchByFlag_ztree_content(treeId, key, value,isFuzzy,flag){
    //<1>.搜索条件
    var searchCondition = value;
    debugger;
    //<2>.得到模糊匹配搜索条件的节点数组集合
    var highlightNodes = new Array();
    if (searchCondition != "") {
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        if(isFuzzy){
            highlightNodes = treeObj.getNodesByParamFuzzy(key, searchCondition, null);

        }else{
            highlightNodes = treeObj.getNodesByParam(key, searchCondition, null);

        }
    }
    return highlightNodes;
    //<3>.高亮显示并展示【指定节点s】
    highlightAndExpand_ztree_content(treeId, highlightNodes, flag);
}

/**
 * 高亮显示并展示【指定节点s】
 * @param treeId
 * @param highlightNodes 需要高亮显示的节点数组
 * @param flag           需要高亮显示的节点标识
 */
function highlightAndExpand_ztree_content(treeId){
    var treeObj = $.fn.zTree.getZTreeObj(treeId);
    //<1>. 先把全部节点更新为普通样式
    var treeNodes = treeObj.transformToArray(treeObj.getNodes());
    for (var i = 0; i < treeNodes.length; i++) {
        treeNodes[i].highlight = false;
        treeObj.updateNode(treeNodes[i]);
    }
    //<2>.收起树, 只展开根节点下的一级节点
    // close_ztree(treeId);
}
function highlight_Expand_ztree_content(treeId){
    var treeObj = $.fn.zTree.getZTreeObj(treeId);
    //<1>. 先把全部节点更新为普通样式
    var treeNodes = treeObj.transformToArray(treeObj.getNodes());
    for (var i = 0; i < treeNodes.length; i++) {
        treeNodes[i].highlight = false;
        treeObj.updateNode(treeNodes[i]);
    }

}

function hightreeBynodes(treeId,highlightNodes,flag) {
    var high=true;
    if(flag){
        high=flag
    }
    var treeObj = $.fn.zTree.getZTreeObj(treeId);
    var treeNodes = treeObj.transformToArray(treeObj.getNodes());
    for (var i = 0; i < highlightNodes.length; i++) {
        highlightNodes[i].highlight = high;
        treeObj.updateNode(highlightNodes[i]);
        //高亮显示节点的父节点的父节点....直到根节点，并展示
        var parentNode = highlightNodes[i].getParentNode();
        // var parentNodes = getParentNodes_ztree(treeId, parentNode);
        // treeObj.expandNode(parentNodes, true, false, true);
        treeObj.expandNode(parentNode, true, false, true);
    }
}
/**
 * 递归得到指定节点的父节点的父节点....直到根节点
 */
var parentNodess=[];
function getParentNodes_ztree(treeId, node){
    if (node != null) {
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        var parentNode = node.getParentNode();
        if(parentNode){
            treeObj.expandNode(parentNode, true, false, true);
        }

        // parentNode.open = true;
        debugger;
        getParentNodes_ztree(treeId, parentNode);
    } else {
        return parentNodess;
    }
}

/**
 * 设置树节点字体样式
 */
function setFontCss_ztree(treeId, treeNode) {
    if (treeNode.id == 0) {
        //根节点
        return {color:"#333", "font-weight":"bold"};
    } else if (treeNode.isParent == false){
        //叶子节点
        return (!!treeNode.highlight) ? {color:"#ff0000", "font-weight":"bold"} : {color:"#660099", "font-weight":"normal"};
    } else {
        //父节点
        return (!!treeNode.highlight) ? {color:"#ff0000", "font-weight":"bold"} : {color:"#333", "font-weight":"normal"};
    }
}
/**
 * 更新树节点数据
 * @param obj 树节点
 * @param online 是否在线 0离线1在线
 * @param speed 速度
 * @param warningType 报警类型
 */
function updateTreeNodeBySoket(obj,online,speed,warningType) {
    debugger;
    //离线
    if(online==0 && warningType==0){
        obj.warningType = "0";
        obj.iconSkin="btnImage iconArea offlineIcon"
    }else if(online==1 && speed>0  && warningType==0){
        // 在线行驶
        obj.warningType = "1";
        obj.iconSkin="btnImage iconArea onlineDriving"

    }else if(online==1 && speed==0 && warningType==0){
        // 在线停止
        obj.warningType = "2";
        obj.iconSkin="btnImage iconArea onlineParking"

    }else if(warningType!=3 && warningType!=0){
        // 报警
        obj.warningType = "3";
        obj.iconSkin="btnImage iconArea warning"

    }else if(warningType==3){
        // 超速
        obj.warningType = "5";
        obj.iconSkin="btnImage iconArea speedLimitWarning"
    }else{
        // 正常
        obj.warningType = "999";
    }
    tree_Obj.updateNode(obj);
}

